package com.example.datadashboard.controller;

import com.example.datadashboard.common.Result;
import com.example.datadashboard.entity.Dashboard;
import com.example.datadashboard.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dashboard")
public class DashboardController {
    @Autowired
    private DashboardService dashboardService;

    @GetMapping("/{id}")
    public Result<Dashboard> getById(@PathVariable Long id) {
        return Result.success(dashboardService.getById(id));
    }

    @GetMapping("/user/{userId}")
    public Result<List<Dashboard>> getUserDashboards(@PathVariable Long userId) {
        return Result.success(dashboardService.getUserDashboards(userId));
    }

    @GetMapping("/public")
    public Result<List<Dashboard>> getPublicDashboards() {
        return Result.success(dashboardService.getPublicDashboards());
    }

    @PostMapping
    public Result<Boolean> save(@RequestBody Dashboard dashboard) {
        return Result.success(dashboardService.save(dashboard));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody Dashboard dashboard) {
        return Result.success(dashboardService.updateById(dashboard));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(dashboardService.removeById(id));
    }
}
